
var json = {
    "openid": "oIWMZ0e2vuDpM9FPQ7R4fJQ8hVHw",
    "vipCard":[
        {
            "type": "vip",
            "name":"成人季度卡",
            "ntatorium": "599552106b4944bbc2039e50",
            "id": "123456",
            "time": {
                "start": 1503306910600,
                "indate": 1513847710600
            }

        },
        {
            "type": "number",
            "name":"30次卡",
            "ntatorium": "599552106b4944bbc2039e50",
            "id": "123451",
            "number": 30

        }
    ],
    "subject":[
        {
            "type": "私教一对一",
            "natatorium": "富怡雅居",
            "team": "高富帅"
        }
    ],
    "clockIn": [
            {
                "natatorium": "富怡雅居",
                "timestamp":1503306910600,
                "cardId": "123456"
            }
        ]
}

var id = '599552106b4944bbc2039e50';

function  checkCard(json, id) {
	//result.type : 0 -> 没有有效卡， 1 -> 打卡成功，2 -> 重复打卡成功 3 -> 重复打卡失败
	var cards = {
		vip:[],
		number:[]
	}, //可以用卡片
		clockInLogArr = [], //如果有打卡的情况用， 临时保存卡片
		cardStatus=false, //是否有当前场馆可用卡片
		cardList = json.vipCard, //卡片列表
		clockIn = null, //最近打卡
		result = {}, // 返回值
		i = 0, //循环变量
		len = 0, //循环变量
		now = new Date().getTime(), //现在的时间戳
		interval = 300000,//5分钟间隔
		clockInLog = false //近期是否打卡
	//检测是否有卡片
	if (cardList.length == 0) return result.type = 0
	
	cardList.map(item => { 
		if (item.ntatorium == id) {
			if (item.type == "vip" && item.time.indate >= now) {
				cards.vip.push(item)
			} else if (item.type == 'number' && item.number > 0) { 
				cards.number.push(item)
			}
			return true
		}
	})
	
	//检测是否有当前场馆可用卡片	
	if (cards.vip.length  == 0 && cards.number.length  == 0 ) return result.type = 0
	//检测近期打卡记录
	if (json.clockIn.length ) {
		if (json.clockIn.timestamp <= now + interval) {
			clockIn = json.clockIn[0]
			clockInLog = false
		}
	}

	//对打卡进行处理
	if (clockInLog) {//有打卡记录
		cards.map(item => {
			if (item.number) {
				clockInLogArr.push(item)
			}
		})

		if (!clockInLogArr.length) return result.type = 3



	} else {//没有打卡记录
		if (cards.vip.length !== 0) {
			result.card = cards.vip[0]
			
		} else {
			result.card = cards.number[0]
		}
		result.type = 1
		return result
	}
}

var log = checkCard(json, id)
console.log(log, 'log')
















